package jui.maps
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.text.StyleSheet;
	
	import jui.business.ManagersFactory;
	import jui.business.StyleManager;
	import jui.maps.events.DataReadyEvent;
	
	import mx.events.FlexEvent;
	import mx.managers.SystemManager;
	
	public class LoadEventMap extends EventDispatcher implements IJUIEventMap
	{
		public var app: JUI;
		public var sysManager: SystemManager;
		public var styleManager: StyleManager;

		
		public function setManagers(): void
		{
			var gm: Function = ManagersFactory.getInstance().getManager;
			
			sysManager = app.systemManager as SystemManager;
			styleManager = gm(StyleManager.getQName());
			//this.xmppManager = new XMPPManager();
		}
		
		/**
		 * What happens when application is complete?
		 * Let's load style sheet
		 */
		public function setHandlers(): void
		{
			sysManager.addEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleteHandler)
				
				// When styles are loaded	
				styleManager.addEventListener(DataReadyEvent.DATA_READY, styleSheetCompleteHandler)
		}
		
		private function applicationCompleteHandler(event:FlexEvent): void
		{
			styleManager.loadStyleSheet();
		}
		
		/**
		 * When style sheet is loaded, let's apply it
		 */
		private function styleSheetCompleteHandler(event: DataReadyEvent): void
		{
			app.messagePane.messageOutput.styleSheet = event.data as StyleSheet;
		}
		
		//-----------------------------------------------------------------------------
		// Singleton implementation
		//-----------------------------------------------------------------------------
		public static var instance: LoadEventMap;
		
		public function LoadEventMap(target:IEventDispatcher=null, app: JUI=null): void
		{
			if(instance) throw new Error("LoadEventMap is singleton");
			
			this.app = app;
			
			setManagers();
			
			setHandlers();
		}
	}
}